package simulation2;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.Map;
import java.util.TreeMap;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;

public class chart extends ApplicationFrame{
	
	private Map<Integer, Integer> mapAllocation;

	public chart(String title) {
		super(title);
		// TODO Auto-generated constructor stub
		mapAllocation = new TreeMap<Integer, Integer>();
		init();
		
		CategoryDataset dataset = createDataset();
		JFreeChart chart = createChart(dataset);
		ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new Dimension(500,270));
		setContentPane(chartPanel);
		
	}
	
	public void init()
	{
		mapAllocation.put(10, 2);
		mapAllocation.put(20, 3);
		mapAllocation.put(30, 6);
		mapAllocation.put(40, 9);
		mapAllocation.put(50, 14);
		mapAllocation.put(60, 18);
		mapAllocation.put(70, 23);
		mapAllocation.put(80, 26);
	}
	
	private CategoryDataset createDataset(){
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		for(Map.Entry entry:mapAllocation.entrySet()){
			dataset.addValue(mapAllocation.get(entry.getKey()), "Classes", entry.getKey()+" ");
		}
		
		return dataset;
	}

	private JFreeChart createChart(CategoryDataset dataset) {
		JFreeChart chart = ChartFactory.createLineChart(
				"Standard PSO", 
				"Number of VM Requests", 
				"NO. of invalid VM Allocations", 
				dataset, 
				PlotOrientation.VERTICAL, 
				false, 
				true, 
				false
				);
		chart.addSubtitle(new TextTitle("test"));
		TextTitle source = new TextTitle(
				"Source: StdPSO "
				+ "by YQP"
				);
		source.setFont(new Font("SansSerif", Font.PLAIN, 10));
		source.setPosition(RectangleEdge.BOTTOM);
		source.setHorizontalAlignment(HorizontalAlignment.RIGHT);
		chart.addSubtitle(source);
		
		chart.setBackgroundPaint(Color.white);
		
		CategoryPlot plot = (CategoryPlot) chart.getPlot();
		plot.setBackgroundPaint(Color.lightGray);
		plot.setRangeGridlinePaint(Color.white);
		
		NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		
		LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
		
		renderer.setShapesVisible(true);
		renderer.setDrawOutlines(true);
		renderer.setUseFillPaint(true);
		renderer.setFillPaint(Color.white);
		
		return chart;
	}
	
}
